

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>scitools.modulecheck &mdash; SciTools 0.8.3 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.8.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="SciTools 0.8.3 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SciTools 0.8.3 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for scitools.modulecheck</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Import a set of modules. If modules are missing,</span>
<span class="sd">one can get these as a comma-separated string, or</span>
<span class="sd">one can raise exceptions.</span>

<span class="sd">See test block for demo of how this module can be used.</span>
<span class="sd">&quot;&quot;&quot;</span>

<div class="viewcode-block" id="missing"><a class="viewcode-back" href="../../modulecheck.html#scitools.modulecheck.missing">[docs]</a><span class="k">def</span> <span class="nf">missing</span><span class="p">(</span><span class="o">*</span><span class="n">module_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Check if modules can be imported.</span>
<span class="sd">    Return a string containing the missing modules.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">missing_modules</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">module</span> <span class="ow">in</span> <span class="n">module_names</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">exec</span><span class="p">(</span><span class="s">&quot;import &quot;</span> <span class="o">+</span> <span class="n">module</span><span class="p">)</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">missing_modules</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">missing_modules</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">s</span>

</div>
<div class="viewcode-block" id="exception"><a class="viewcode-back" href="../../modulecheck.html#scitools.modulecheck.exception">[docs]</a><span class="k">def</span> <span class="nf">exception</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="o">*</span><span class="n">module_names</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Raise an ImportError exception if modules</span>
<span class="sd">    are missing. Print the names of the missing modules.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">missing</span><span class="p">(</span><span class="o">*</span><span class="n">module_names</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">s</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> missing the module(s) </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">msg</span><span class="p">,</span><span class="n">s</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="message"><a class="viewcode-back" href="../../modulecheck.html#scitools.modulecheck.message">[docs]</a><span class="k">def</span> <span class="nf">message</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">critical</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Import a module and write a message if it is missing.</span>
<span class="sd">    critical=0 means that the module is not critical</span>
<span class="sd">    (programs may work without). critical=1 means that the</span>
<span class="sd">    module must be installed.</span>
<span class="sd">    msg is an optional description of the module.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">exec</span><span class="p">(</span><span class="s">&quot;import &quot;</span><span class="o">+</span> <span class="n">module</span><span class="p">)</span>
        <span class="k">print</span> <span class="s">&quot;You have the Python&quot;</span><span class="p">,</span> <span class="n">module</span><span class="p">,</span> <span class="s">&quot;module installed...&quot;</span>
        <span class="n">success</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;*** The&quot;</span><span class="p">,</span> <span class="n">module</span><span class="p">,</span> <span class="s">&quot;Python module is not available...&quot;</span>
        <span class="n">success</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;    (</span><span class="si">%s</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="n">msg</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">critical</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;       ....but this is not critical&quot;</span>
            <span class="n">success</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="k">return</span> <span class="n">success</span>

<span class="c"># Pmw.Blt.Graph requires a special treatment:</span></div>
<div class="viewcode-block" id="PmwBlt"><a class="viewcode-back" href="../../modulecheck.html#scitools.modulecheck.PmwBlt">[docs]</a><span class="k">def</span> <span class="nf">PmwBlt</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="c"># check if we manage to create a Blt graph:</span>
        <span class="kn">import</span> <span class="nn">Pmw</span><span class="p">;</span>  <span class="n">s</span> <span class="o">=</span> <span class="n">Pmw</span><span class="o">.</span><span class="n">Blt</span><span class="o">.</span><span class="n">Graph</span>
        <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;You have the Python Pmw.Blt.Graph widget available...&quot;</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;*** Python is not correctly linked with BLT (no Pmw.Blt.Graph widget)&quot;</span>
        <span class="k">return</span> <span class="bp">False</span>

</div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="c"># Suppose we have a class SomeClass requiring the modules</span>
    <span class="c"># A, B, and C. The following code segment shows how you can</span>
    <span class="c"># import SomeClass silently if one or more of the modules A, B, or</span>
    <span class="c"># C are missing, and then get the error message of a missing</span>
    <span class="c"># module when you create an instance of the class</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">A</span><span class="o">,</span> <span class="nn">B</span><span class="o">,</span> <span class="nn">C</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="k">class</span> <span class="nc">SomeClass</span><span class="p">:</span>
        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
            <span class="kn">import</span> <span class="nn">modulecheck</span>
            <span class="n">modulecheck</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="s">&quot;SomeClass:&quot;</span><span class="p">,</span> <span class="s">&quot;A&quot;</span><span class="p">,</span> <span class="s">&quot;B&quot;</span><span class="p">,</span> <span class="s">&quot;C&quot;</span><span class="p">)</span>

            <span class="c"># if we come here, no exception from modulecheck.exception</span>
            <span class="c"># was raised so we can continue with the constructor statements,</span>
            <span class="c"># e.g.</span>
            <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="n">b</span> <span class="o">=</span> <span class="mi">2</span>

    <span class="c"># this should raise an exeption! (as the modules A, B, and C are missing)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">sys</span>
        <span class="k">print</span> <span class="s">&quot;Test worked; &quot;</span><span class="p">,</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_type</span><span class="p">,</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_value</span>

    <span class="c"># this should work fine:</span>
    <span class="k">if</span> <span class="n">exception</span><span class="p">(</span><span class="s">&quot;just testing...&quot;</span><span class="p">,</span> <span class="s">&quot;os&quot;</span><span class="p">):</span>  <span class="k">print</span> <span class="s">&quot;bug!&quot;</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/scitools_logo.jpg" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SciTools 0.8.3 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, H. P. Langtangen, J. Ring, ++.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>